define(function (require, exports, module) {
    var angular = require('angular');
    require("pagination");
    require("angular-ui");
    var util = require("util");
    var api_prefix = G_CONFIG["api_prefix"]; //api前缀

    angular.module('ParamsInfor',[])
        .controller('paramsCarryoverCtrl',['$scope','$location', '$routeParams',function($scope,$location, $routeParams){

            $scope.saveCarryoverTime = function (record) {
                var url = api_prefix + "/params/save-params-carryover-time";

                var bgtime = record.carryover_begin_time;
                var endtime = record.carryover_end_time;
                if(bgtime == null || endtime == null || bgtime == '' || endtime == ''){
                    util.alert("请设置开始时间和结束时间!");
                    return;
                }
                if(bgtime>=endtime){
                    util.alert("开始时间必须小于结束时间!");
                    return;
                }

                util.myAjax({
                    url: url,
                    type: "POST",
                    data: {carryovertime:record},
                    success: function (json) {
                        if (json.retCode === 0) {
                            util.alert("保存成功!");
                            $scope.query.execute();
                        } else {
                            util.alert("保存失败：" + json.errMsg);
                        }
                    }
                });
            };

            $scope.cancelCarryoverTime = function (record) {
                angular.extend(record,$scope.orginalData);
                record.editMode = 0;
            };
            
            $scope.editCarryoverTime = function (record) {
                var url = api_prefix + "/params/get-can-change-carryover-time";
                util.myAjax({
                    url: url,
                    type: "POST",
                    data: {},
                    success: function (json) {
                        if (json.retCode === 0) {
                            record.editMode = 2;
                            $("#btnUpdate").click();
                        } else {
                            util.alert("待确认收入项列表中存在未结转的收入项，请结转后再调整结转期间！");
                        }
                    }
                });
            };

            $scope.query = {
                execute: function(){
                    var url = api_prefix + "/params/get-params-carryover-time";

                    util.myAjax({
                        url: url,
                        type: "POST",
                        data: {},
                        success: function (json) {
                            if (json.retCode === 0) {
                                $scope.params = json.data;
                                if(json.data[0]){
                                    $scope.orginalData = angular.copy(json.data[0]);
                                }
                                $scope.$applyAsync();
                            } else {
                                util.alert("获取参数失败：" + json.errMsg);
                            }
                        }
                    });
                }
            };

            $scope.query.execute();
        }])
})